﻿using JuCloudy.Data.Entity;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace JuCloudy.Sys.App.Structure
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
            this.Loaded += (obj, e) =>
            {
                this.InitData();
                this.OnLoad();
            };
        }

        private JuCloudy.Framework.Interface.IStructure Structure =
                   (JuCloudy.Framework.Interface.IStructure)Application.Current.RootVisual;

        private ObservableCollection<SysStructure> entities
        {
            get { return new ObservableCollection<SysStructure>((IEnumerable<Data.Entity.SysStructure>)this._dg.ItemsSource); }
            set { this._dg.ItemsSource = value; }
        }

        private void InitData()
        {
            //Structure.ShowLoading();
            SystemService.WSSysServiceSoapClient client
                = new SystemService.WSSysServiceSoapClient();
            client.GetStructureEntitiesAsync();
            client.GetStructureEntitiesCompleted += (obj, ex) =>
            {
                //Structure.CloseLoading();
                if (ex.Error != null)
                    throw ex.Error;

                this.entities = ex.Result.Deserialize(typeof(ObservableCollection<SysStructure>))
                    as ObservableCollection<SysStructure>;

            };
        }

        public void insert(SysStructure entity)
        {
            Structure.ShowLoading();
            SystemService.WSSysServiceSoapClient client
                = new SystemService.WSSysServiceSoapClient();
            client.InsertStructureAsync(entity.Serialize());
            client.InsertStructureCompleted += (obj, ex) =>
            {
                Structure.CloseLoading();
                if (ex.Error != null)
                    throw ex.Error;

                this.entities = ex.Result.Deserialize(typeof(ObservableCollection<SysStructure>))
                    as ObservableCollection<SysStructure>;
                Structure.CloseChildDialog();
            };
        }

        public void delete(SysStructure entity)
        {
            Structure.ShowLoading();
            SystemService.WSSysServiceSoapClient client
                = new SystemService.WSSysServiceSoapClient();
            client.DeleteStructureAsync(entity.Serialize());
            client.DeleteStructureCompleted += (obj, ex) =>
            {
                Structure.CloseLoading();
                if (ex.Error != null)
                    throw ex.Error;

                this.entities = ex.Result.Deserialize(typeof(ObservableCollection<SysStructure>))
                    as ObservableCollection<SysStructure>;
                Structure.CloseChildDialog();
            };
        }

        private void OnLoad()
        {
            this._insertbtn.Click += (obj, e) =>
            {
                var dialog = new controls.insert();
                dialog.Mainpage = this;
                Structure.ShowChildDialog(dialog, "新增架构");
            };

            this._updatabtn.Click += (obj, e) =>
            {
                var dialog = new controls.insert();
                dialog.Mainpage = this;
                dialog.Entity = this._dg.SelectedItem as SysStructure;
                Structure.ShowChildDialog(dialog, "编辑架构");
            };

            this._dg.SelectionChanged+=_dg_SelectionChanged;
        }

        private void _dg_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (e.AddedItems != null && e.AddedItems.Count > 0)
            {
                this._updatabtn.IsEnabled = true;
                this._deletebtn.IsEnabled = true;
            }
            else
            {
                this._updatabtn.IsEnabled = false;
                this._deletebtn.IsEnabled = false;
            }
        }
    }
}
